package com.xu.t1.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.Serial;
import java.io.Serializable;

public class UploadLog implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    // Jackson序列化工具（用于内部类转 JSON）
    private static final ObjectMapper mapper = new ObjectMapper();

    // 唯一日志 ID（每条日志记录的唯一标识，可使用 UUID 或雪花算法生成）
    private String logId;

    // 日志生成时间戳（毫秒级，方便排序和时间分析）
    private long timestamp;

    // 当前日志来源服务器的标识（用于分布式环境中区分不同节点）
    private String serverId;

    // 本次操作的统一指令 ID（用于把一次分布式操作的所有日志归类在一起）
    private String instructionId;

    // 操作发起者的标识（可以是用户ID、IP地址或调度器ID）
    private String userId;

    // 实际分配的线程数量（用于记录本次操作使用了多少线程）
    private int threadCount;

    // 本次操作耗时（单位：毫秒，通常用于性能分析）
    private long durationMs;

    // 分布式锁相关信息（记录是否成功加锁、尝试次数等）
    private DistributedLockInfo distributedLock;

    // 乐观锁相关信息（记录重试次数、是否更新成功等）
    private OptimisticLockInfo optimisticLock;

    // 附加信息（可用于记录成功/失败说明、异常消息、自定义提示等）
    private String message;

    // Getter / Setter 省略注释

    public String getLogId() {
        return logId;
    }
    public void setLogId(String logId) {
        this.logId = logId;
    }

    public long getTimestamp() {
        return timestamp;
    }
    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String getServerId() {
        return serverId;
    }
    public void setServerId(String serverId) {
        this.serverId = serverId;
    }

    public String getInstructionId() {
        return instructionId;
    }
    public void setInstructionId(String instructionId) {
        this.instructionId = instructionId;
    }

    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getThreadCount() {
        return threadCount;
    }
    public void setThreadCount(int threadCount) {
        this.threadCount = threadCount;
    }

    public long getDurationMs() {
        return durationMs;
    }
    public void setDurationMs(long durationMs) {
        this.durationMs = durationMs;
    }

    public DistributedLockInfo getDistributedLock() {
        return distributedLock;
    }
    public void setDistributedLock(DistributedLockInfo distributedLock) {
        this.distributedLock = distributedLock;
    }

    public OptimisticLockInfo getOptimisticLock() {
        return optimisticLock;
    }
    public void setOptimisticLock(OptimisticLockInfo optimisticLock) {
        this.optimisticLock = optimisticLock;
    }

    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }

    // 分布式锁 - JSON序列化
    public String getDistributedLockJson() {
        try {
            return distributedLock == null ? null : mapper.writeValueAsString(distributedLock);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    // 乐观锁 - JSON序列化
    public String getOptimisticLockJson() {
        try {
            return optimisticLock == null ? null : mapper.writeValueAsString(optimisticLock);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    //  内部类：DistributedLockInfo
    public static class DistributedLockInfo implements Serializable {
        private int tryCount;
        private boolean success;

        public int getTryCount() {
            return tryCount;
        }
        public void setTryCount(int tryCount) {
            this.tryCount = tryCount;
        }
        public boolean isSuccess() {
            return success;
        }
        public void setSuccess(boolean success) {
            this.success = success;
        }

        @Override
        public String toString() {
            return "DistributedLockInfo{" +
                    "tryCount=" + tryCount +
                    ", success=" + success +
                    '}';
        }
    }

    // 内部类：OptimisticLockInfo
    public static class OptimisticLockInfo implements Serializable {
        private int tryCount;
        private boolean success;

        public int getTryCount() {
            return tryCount;
        }
        public void setTryCount(int tryCount) {
            this.tryCount = tryCount;
        }
        public boolean isSuccess() {
            return success;
        }
        public void setSuccess(boolean success) {
            this.success = success;
        }

        @Override
        public String toString() {
            return "OptimisticLockInfo{" +
                    "tryCount=" + tryCount +
                    ", success=" + success +
                    '}';
        }
    }

    @Override
    public String toString() {
        return "UploadLogService{" +
                "logId='" + logId + '\'' +
                ", timestamp=" + timestamp +
                ", serverId='" + serverId + '\'' +
                ", instructionId='" + instructionId + '\'' +
                ", userId='" + userId + '\'' +
                ", threadCount=" + threadCount +
                ", durationMs=" + durationMs +
                ", distributedLock=" + distributedLock +
                ", optimisticLock=" + optimisticLock +
                ", message='" + message + '\'' +
                '}';
    }
}